package com.xunxi.mimi.utils;

import java.util.Collection;
import java.util.List;

/**
 * Created by lmy on 2018/3/28.
 */

public class ListUtil{
    private ListUtil() {
    }

    public static int getSize(Collection<?> collection) {
        return collection != null?collection.size():0;
    }

    public static <T> T getItemSafely(List<T> list, int position) {
        return list != null && list.size() > position?list.get(position):null;
    }

    public static boolean isEmpty(Collection<?> collection) {
        return collection == null || collection.size() == 0;
    }

    public static <T> int sizeOfSubList(List<T> list, int row, int page) {
        List<T> subList = getSubList(list, row, page);
        return getSize(subList);
    }

    public static <T> List<T> getSubList(List<T> list, int row, int page) {
        if(list != null) {
            int N = list.size();
            int start = row * page;
            int end = Math.min(N, start + row);
            if(start < N) {
                return list.subList(start, end);
            }
        }

        return null;
    }

}
